home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP06 / CHECKER3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  4.7 KB  |  142 lines

  1. /*-------------------------------------------------
  2.    CHECKER3.C -- Mouse Hit-Test Demo Program No. 3
  3.                  (c) Charles Petzold, 1996
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define DIVISIONS 5
  9. #define MoveTo(hdc, x, y) MoveToEx (hdc, x, y, NULL)
  10.  
  11. LRESULT CALLBACK WndProc   (HWND, UINT, WPARAM, LPARAM) ;
  12. LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;
  13.  
  14. char      szChildClass[] = "Checker3_Child" ;
  15.  
  16. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  17.                     PSTR szCmdLine, int iCmdShow)
  18.      {
  19.      static char szAppName[] = "Checker3" ;
  20.      HWND        hwnd ;
  21.      MSG         msg ;
  22.      WNDCLASSEX  wndclass ;
  23.  
  24.      wndclass.cbSize        = sizeof (wndclass) ;
  25.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  26.      wndclass.lpfnWndProc   = WndProc ;
  27.      wndclass.cbClsExtra    = 0 ;
  28.      wndclass.cbWndExtra    = 0 ;
  29.      wndclass.hInstance     = hInstance ;
  30.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  31.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  32.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  33.      wndclass.lpszMenuName  = NULL ;
  34.      wndclass.lpszClassName = szAppName ;
  35.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  36.  
  37.      RegisterClassEx (&wndclass) ;
  38.  
  39.      wndclass.lpfnWndProc   = ChildWndProc ;
  40.      wndclass.cbWndExtra    = sizeof (WORD) ;
  41.      wndclass.hIcon         = NULL ;
  42.      wndclass.lpszClassName = szChildClass ;
  43.      wndclass.hIconSm       = NULL ;
  44.  
  45.      RegisterClassEx (&wndclass) ;
  46.  
  47.      hwnd = CreateWindow (szAppName, "Checker3 Mouse Hit-Test Demo",
  48.                          WS_OVERLAPPEDWINDOW,
  49.                          CW_USEDEFAULT, CW_USEDEFAULT,
  50.                          CW_USEDEFAULT, CW_USEDEFAULT,
  51.                          NULL, NULL, hInstance, NULL) ;
  52.  
  53.      ShowWindow (hwnd, iCmdShow) ;
  54.      UpdateWindow (hwnd) ;
  55.  
  56.      while (GetMessage (&msg, NULL, 0, 0))
  57.           {
  58.           TranslateMessage (&msg) ;
  59.           DispatchMessage (&msg) ;
  60.           }
  61.      return msg.wParam ;
  62.      }
  63.  
  64. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  65.      {
  66.      static HWND hwndChild[DIVISIONS][DIVISIONS] ;
  67.      int         cxBlock, cyBlock, x, y ;
  68.  
  69.      switch (iMsg)
  70.           {
  71.           case WM_CREATE :
  72.                for (x = 0 ; x < DIVISIONS ; x++)
  73.                     for (y = 0 ; y < DIVISIONS ; y++)
  74.                          {
  75.                          hwndChild[x][y] = CreateWindow (szChildClass, NULL,
  76.                               WS_CHILDWINDOW | WS_VISIBLE,
  77.                               0, 0, 0, 0,
  78.                               hwnd, (HMENU) (y << 8 | x),
  79.                               (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
  80.                               NULL) ;
  81.                          }
  82.                return 0 ;
  83.  
  84.           case WM_SIZE :
  85.                cxBlock = LOWORD (lParam) / DIVISIONS ;
  86.                cyBlock = HIWORD (lParam) / DIVISIONS ;
  87.  
  88.                for (x = 0 ; x < DIVISIONS ; x++)
  89.                     for (y = 0 ; y < DIVISIONS ; y++)
  90.                          MoveWindow (hwndChild[x][y],
  91.                               x * cxBlock, y * cyBlock,
  92.                               cxBlock, cyBlock, TRUE) ;
  93.                return 0 ;
  94.  
  95.           case WM_LBUTTONDOWN :
  96.                MessageBeep (0) ;
  97.                return 0 ;
  98.  
  99.           case WM_DESTROY :
  100.                PostQuitMessage (0) ;
  101.                return 0 ;
  102.           }
  103.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  104.      }
  105.  
  106. LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  107.      {
  108.      HDC         hdc ;
  109.      PAINTSTRUCT ps ;
  110.      RECT        rect ;
  111.  
  112.      switch (iMsg)
  113.           {
  114.           case WM_CREATE :
  115.                SetWindowWord (hwnd, 0, 0) ;       // on/off flag
  116.                return 0 ;
  117.  
  118.           case WM_LBUTTONDOWN :
  119.                SetWindowWord (hwnd, 0, 1 ^ GetWindowWord (hwnd, 0)) ;
  120.                InvalidateRect (hwnd, NULL, FALSE) ;
  121.                return 0 ;
  122.  
  123.           case WM_PAINT :
  124.                hdc = BeginPaint (hwnd, &ps) ;
  125.  
  126.                GetClientRect (hwnd, &rect) ;
  127.                Rectangle (hdc, 0, 0, rect.right, rect.bottom) ;
  128.  
  129.                if (GetWindowWord (hwnd, 0))
  130.                     {
  131.                     MoveTo (hdc, 0,          0) ;
  132.                     LineTo (hdc, rect.right, rect.bottom) ;
  133.                     MoveTo (hdc, 0,          rect.bottom) ;
  134.                     LineTo (hdc, rect.right, 0) ;
  135.                     }
  136.  
  137.                EndPaint (hwnd, &ps) ;
  138.                return 0 ;
  139.           }
  140.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  141.      }
  142.